﻿@{
    ViewData["Title"] = "重置用户密码";
    Layout = "~/Views/Shared/_LayoutNone.cshtml";
}
<div class="layui-form" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list" style="padding: 20px 30px 0 0;">
    <div class="layui-form-item">
        <label class="layui-form-label"><span style="color:red">*</span> 新密码</label>
        <div class="layui-input-inline">
            <input type="password" id="new_password" name="new_password" maxlength="50" lay-verify="required|pass" placeholder="请输入密码" autocomplete="off" class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux">请填写6到30位密码!</div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"><span style="color:red">*</span> 确认密码</label>
        <div class="layui-input-inline">
            <input type="password" id="confirm_password" name="confirm_password" maxlength="50" lay-verify="required|pass" placeholder="请输入密码" autocomplete="off" class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux">请填写6到30位密码!</div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"></label>
        <div class="layui-input-block" style="color: red;">
            提醒：密码必须是6到30位、包含字母、数字和符号！
        </div>
    </div>
    <div class="layui-form-item layui-hide">
        <input type="button" lay-submit lay-filter="layuiadmin-app-form-submit" id="layuiadmin-app-form-submit" value="确认添加">
        <input type="button" lay-submit lay-filter="layuiadmin-app-form-edit" id="layuiadmin-app-form-edit" value="确认编辑">
    </div>
</div>

<script src="~/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/layuiadmin/'
    }).extend({
        index: '/lib/index'
    }).use(['index', 'form'], function () {
        var $ = layui.$
            , form = layui.form
            , admin = layui.admin;

        // 验证表单
        form.verify({
            pass: [/^(?:(?=.*[0-9].*)(?=.*[A-Za-z].*)(?=.*[\W].*))[\W0-9A-Za-z]{6,30}$/, '密码必须6到30位、包含字母、数字和符号，且不能出现空格']
        });

        // 监听提交
        form.on('submit(layuiadmin-app-form-edit)', function (data) {
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            loading = layer.load(2, {
                shade: [0.2, '#000']
            });

            if (data.field.confirm_password != data.field.new_password) {
                layer.close(loading);
                layer.msg('两次输入密码不一致！', {
                    offset: '15px'
                    , icon: 1
                    , time: 2000
                });
                return;
            }

            var id = @ViewContext.HttpContext.Request.Query["id"];

            // 提交数据
            admin.req({
                headers: {
                    'RequestVerificationToken': csrfToken
                },
                method: 'POST',
                url: '/users/resetpassword?new_password=' + data.field.news_password + '&id=' + id
                , data: data.field
                , done: function (res) {
                    // console.log(`res=${JSON.stringify(res)}`);
                    layer.close(loading);
                    layer.msg(res.msg, {
                        offset: '15px'
                        , icon: 1
                        , time: 2000
                    }, function () {
                            layer.close(loading);
                            parent.layui.table.reload('LAY-app-content-list'); //重载表格
                            layer.msg(res.msg, {
                                offset: '15px'
                                , icon: 1
                                , time: 2000
                            }, function () {
                                parent.layer.close(index); //再执行关闭
                            });
                    });
                }
            });
        });
    })
</script>
